package io.renren.common.rpc.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.renren.common.constants.DbConst;
import io.renren.common.utils.CommonUtil;
import io.renren.common.utils.WxUtils;
import org.springframework.context.annotation.Bean;

/**
 * @Auther: miaoguoxin
 * @Date: 2019/11/17 19:38
 * @Description: feign调用权限拦截器
 */
public class FeignAuthConfig {
    @Bean
    public RequestInterceptor authIntercept() {
        return requestTemplate -> {
            String randomString = CommonUtil.getRandomString(30);
            String reqBodyJson = requestTemplate.requestBody().asString();
            JSONObject params = null;
            try {
                params = JSON.parseObject(reqBodyJson);
            } catch (Exception e) {

            }
            if (params == null) {
                params = new JSONObject();
                params.put("nonceStr", randomString);
            }
            String sign = WxUtils.createSign(params, DbConst.OUT_REST_KEY);
            requestTemplate.header("nonce_str", randomString)
                    .header("sign",sign);
        };
    }


}
